package com.uwantsoft.business.infrastructure;

import com.uwantsoft.business.service.infrastructure.AppPusher;
import com.uwantsoft.constant.Constant;
import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Chuanbao
 */
public class IosPusher implements AppPusher {
    private Logger log = Logger.getLogger(IosPusher.class);
    private String certificatePath = "";
    private String certificatePassword = "uwantsoft123";

    @Override
    public boolean supported(String deviceType) {
        return Constant.DEVICE_IOS.equalsIgnoreCase(deviceType);
    }

    @Override
    public void pushToSingleDevice(String deviceType, String deviceToken, String message) throws Exception {
        int badge = 100;//图标小红圈的数值
        String sound = "default";//铃音

        List<String> tokens = new ArrayList<String>();
        tokens.add(deviceToken);
        boolean sendCount = true;

        try {
            PushNotificationPayload payLoad = new PushNotificationPayload();
            payLoad.addAlert(message); // 消息内容
            payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值
            if (!StringUtils.isEmpty(sound)) {
                payLoad.addSound(sound);//铃音
            }
            PushNotificationManager pushManager = new PushNotificationManager();
            //true：表示的是产品发布推送服务 false：表示的是产品测试推送服务
            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));
            List<PushedNotification> notifications = new ArrayList<PushedNotification>();
            // 发送push消息
            Device device = new BasicDevice();
            device.setToken(tokens.get(0));
            PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
            notifications.add(notification);
            pushManager.stopConnection();
        } catch (Exception e) {
            log.error("Push to single IOS device[deviceToken:"+deviceToken+"] failed.");
            throw e;
        }
    }

    @Override
    public void pushToGroup(String deviceType, String[] deviceTokens, String message) throws Exception {
        String alert = "我的push测试";//push的内容
        int badge = 100;//图标小红圈的数值
        String sound = "default";//铃音

        List<String> tokens = new ArrayList<String>();
        tokens.addAll(Arrays.asList(deviceTokens));
        try {
            PushNotificationPayload payLoad = new PushNotificationPayload();
            payLoad.addAlert(alert); // 消息内容
            payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值
            if (!StringUtils.isEmpty(sound)) {
                payLoad.addSound(sound);//铃音
            }
            PushNotificationManager pushManager = new PushNotificationManager();
            //true：表示的是产品发布推送服务 false：表示的是产品测试推送服务
            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));
            List<PushedNotification> notifications = new ArrayList<PushedNotification>();
            // 发送push消息

            List<Device> device = new ArrayList<Device>();
            for (String token : tokens) {
                device.add(new BasicDevice(token));
            }
            notifications = pushManager.sendNotifications(payLoad, device);

            List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
            List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
            int failed = failedNotifications.size();
            int successful = successfulNotifications.size();
            pushManager.stopConnection();
        } catch (Exception e) {
            log.error("Push to single IOS device[deviceTokens:"+StringUtils.arrayToDelimitedString(deviceTokens,",")+"] failed.");
            throw e;
        }
    }

    @Override
    public void pushToTag(String deviceType, String tag, String message) throws Exception {

    }
}
